﻿namespace Central.Linq {

	/// <summary>
	/// Класс результата агрегирующей функции
	/// </summary>
	public class AggregateFunctionResult : AggregateFunctionDefinition {
		#region Конструкторы

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionResult()
			: base() { }

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionResult(AggregateFunction function)
			: base(function) { }

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionResult(AggregateFunction function, object value)
			: base(function) {
			this.Value = value;
		}

		#endregion Конструкторы

		/// <summary>
		/// Результат агрегирующей функции
		/// </summary>
		public object Value { get; set; }

		#region методы

		/// <summary>
		/// Сравнение объектов
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public override bool Equals(object obj) {
			return Equals(obj as AggregateFunctionResult);
		}

		/// <summary>
		/// Сравнение объектов AggregateFunctionDefinition
		/// </summary>
		/// <param name="other"></param>
		/// <returns></returns>
		public bool Equals(AggregateFunctionResult other) {
			if (other == null)
				return false;

			return other.Function.Equals(this.Function) && ((other.Value == null) == (this.Value == null)) && (other.Value == null || other.Value.Equals(this.Value));
		}

		/// <summary>
		/// Получение Hash кода объекта
		/// </summary>
		/// <returns></returns>
		public override int GetHashCode() {
			//Calculate the hash code
			if (this.Value == null)
				return this.Function.GetHashCode();
			else
				return this.Function.GetHashCode() ^ this.Value.GetHashCode();
		}

		#endregion методы
	}
}